package client_controllers;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.util.ArrayList;
import client_gui.MaintainAreaGUI;
import client_logic.CcrmClient;
import client_logic.dbHandler;
import entity.Area;
import entity.Boss;

/**
 * @class MaintainAreaController ,in this class the marketing manager add a new area to the db.
 * 
 * @param MaintainAreaGUI links to GUI.
 * @param EmployeeLoginController links to employee controller. 
 * @param Boss is used as an instance, to send objects to DB and allow changes in the lists.
 * @param MarketingManagerController links to marketing manager controller.
 */
public class MaintainAreaController implements dbHandler {
	private Boss b;
	MaintainAreaGUI mag;
	MarketingManagerController mmc;
	EmployeeLoginController elc;
	
	public MaintainAreaController(MaintainAreaGUI mag,MarketingManagerController mmc,EmployeeLoginController elc){
		this.mag=mag;
		this.mmc=mmc;
		this.elc=elc;
		mag.getBtnAddArea().addActionListener(new AddBtn());
		mag.getBtnBack().addActionListener(new BackBtn());
		mag.getBtnLogout().addActionListener(new Logout());
		mag.addWindowListener(new Exit());
		mag.getBtnCreateArea().addActionListener(new CreateBtn());
		mag.getBtnDeleteArea().addActionListener(new DeleteBtn());
		mag.getBtnDelete().addActionListener(new Delete());
	}
	
	public Boss getB() {
		return b;
	}


	public void setB(Boss b) {
		this.b = b;
	}
	
	public class Logout implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        		setB(new Boss(getElc().getEmp(),"logout","MaintainAreaController"));
        		update();
	
        	}
	 }
	public class AddBtn implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        		mag.getPaneldelete().setVisible(false);
        		mag.getPaneladd().setVisible(true);
        	}
	 }
	public class DeleteBtn implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        		mag.getAreacomboBox().removeAllItems();
        		getCombo();
        		mag.getPaneladd().setVisible(false);
        		mag.getPaneldelete().setVisible(true);
        	}
	 }
	public class BackBtn implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        		mag.dispose();
        		mmc.getMmg().setVisible(true);
        	}
	 }
	public class Delete implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        		Area area=new Area();
        		area.setAreaName(mag.getAreacomboBox().getSelectedItem().toString());
        		setB(new Boss(area,"deletearea","MaintainAreaController"));
        		Delete();
        	}
	 }
	public class CreateBtn implements ActionListener {
        public void actionPerformed(ActionEvent ev){
        	setB(new Boss(null,"",""));
        		Area area=new Area();
        		try {
					area.setAid(Integer.parseInt(mag.getAreaIdtextField().getText()));
					area.setAreaName(mag.getAreaNametextfield().getText());
				} catch (NumberFormatException e) {
					mag.getLblerror().setVisible(true);
				}
        		if(area.getAreaName().equals(""))
        			mag.getLblerror().setVisible(true);
        		else {
        			b.setObj(area);
        			b.setCommand("addarea");
        			b.setControl("MaintainAreaController");
        			Add();
        		}
        	}    	
	 }

	public void Add() {
try {
			
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}

	public void Search() {
		try {
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String Delete() {
		try {
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * the function HandleMsg get massage from server
	 * @param msg it item from the DB Usually comes as Boss
	 */
	public void HandleMsg(Object msg) {
		this.setB((Boss) msg);
		if(this.getB().getMsg().equals("logout")){
			this.getMag().dispose();
			this.getElc().getElg().setVisible(true);
		}
		else if(this.getB().getMsg().equals("area already exist")){
			mag.getLblerror().setForeground(Color.red);
			mag.getLblerror().setText("Area Already Exist");
			mag.getLblerror().setVisible(true);
			
		}
		else if(this.getB().getMsg().equals("areaadded")){
			mag.getLblerror().setText("Area Added Successfully");
			mag.getLblerror().setForeground(Color.blue);
			mag.getLblerror().setVisible(true);
		}
		else if(this.getB().getCommand().equals("getCombo")){
			ArrayList <String> areas ;
			areas= (ArrayList<String>) this.getB().getObj();
			for(String i :areas )
				mag.getAreacomboBox().addItem(i);
		}
		else if(this.getB().getMsg().equals("deletedsuccessfully")){
			mag.getLblAreaDeletedSuccessfully().setVisible(true);
			mag.getAreacomboBox().removeItem(mag.getAreacomboBox().getSelectedItem());
		}
		
	}

	public void update() {
		try {
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public class Exit implements WindowListener{

		@Override
		public void windowActivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowClosed(WindowEvent e) {
			
			
		}

		@Override
		public void windowClosing(WindowEvent e) {
			
			setB(new Boss(getElc().getEmp(),"logout","MaintainAreaController"));
    		update();
			
		}

		@Override
		public void windowDeactivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowDeiconified(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowIconified(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowOpened(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}

	public MaintainAreaGUI getMag() {
		return mag;
	}


	public void setMag(MaintainAreaGUI mag) {
		this.mag = mag;
	}


	public MarketingManagerController getMmc() {
		return mmc;
	}


	public void setMmc(MarketingManagerController mmc) {
		this.mmc = mmc;
	}


	public EmployeeLoginController getElc() {
		return elc;
	}


	public void setElc(EmployeeLoginController elc) {
		this.elc = elc;
	}
	/**
	 * in this function i sent request for combo of area
	 */
	public void getCombo(){
		this.setB(new Boss(null,"getCombo","MaintainAreaController"));
		this.Search();
	}
}

